<template>
<el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dept">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item>
                <ren-select v-model="dataForm.type" dict-type="bigData_type" placeholder="请选择模型类型" style="width:100%"></ren-select>
            </el-form-item>
            <el-form-item>
                <el-input v-model="dataForm.name" placeholder="模型名称" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="dataForm.version" placeholder="版本号" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()">{{ $t("query") }}</el-button>
            </el-form-item>
            <!-- <el-form-item>
                <el-button type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
            </el-form-item> -->
            <el-form-item>
                <el-button v-if="$hasPermission('sys:bdmc:delete')"  type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle" style="width: 100%">
            <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
            <el-table-column prop="version" label="版本号" header-align="center" align="center"></el-table-column>
            <el-table-column prop="name" label="模型名称" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="type" label="模型数据类型" header-align="center" align="center">
                <template slot-scope="scope">
                    {{$getDictLabel('bigData_type',scope.row.type)}}
                </template>
            </el-table-column>
            <el-table-column prop="releaseStatus" label="发布状态" header-align="center" align="center">
                <template slot-scope="scope">
                    <el-button :type="scope.row.releaseStatus!=1?'info':'success'" size="mini"> {{scope.row.releaseStatus==0?'未发布':'已发布'}}</el-button>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="status" label="状态" header-align="center" align="center">
                <template slot-scope="scope">
                    <el-switch @change="(val)=>changeStatus(scope.row.id,val)" :value="scope.row.status" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ddd" active-text="启用" inactive-text="禁用">
                    </el-switch>
                </template>
            </el-table-column> -->
            <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column>
            <el-table-column prop="updateDate" label="修改时间" header-align="center" align="center"></el-table-column>
            <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="180">
                <template slot-scope="scope">
                    <el-button v-if="$hasPermission('sys:bdmc:info')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">查看</el-button>
                    <el-button v-if="$hasPermission('sys:vmc:recall')" type="text" size="small" @click="backTo(scope.row.id)">回溯</el-button>
                    <el-button v-if="$hasPermission('sys:bdmc:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
</el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./version-add-or-update";
export default {
    mixins: [mixinViewModule],
    data() {
        return {
            mixinViewModuleOptions: {
                getDataListURL: "/sys/vmc/page",
                deleteURL: "/sys/vmc",
                getDataListIsPage: true,
                deleteIsBatch: true,
            },
            dataForm: {
                type:'',
                name:'',
                version:''
            },
        };
    },
    components: {
        AddOrUpdate,
    },
    methods: {
        changeStatus(id, status) {
            this.$confirm(`确定该${status?'启用':'禁用'}？`, this.$t("prompt.title"), {
                    confirmButtonText: this.$t("confirm"),
                    cancelButtonText: this.$t("cancel"),
                    type: "warning",
                })
                .then(() => {
                    this.$http
                        .put('/sys/vmc', {
                            id: id,
                            status: status,
                             releaseStatus:status
                        })
                        .then((res) => {
                            if (res.data.code !== 0) {
                                return this.$message.error(res.data.msg);
                            }
                            this.$message({
                                message: this.$t("prompt.success"),
                                type: "success",
                                duration: 500,
                                onClose: () => {},
                            });
                        })
                }).catch(() => {
                    console.log(1)
                });
        },
        //回溯
        backTo(id) {
            this.$confirm("【谨慎】此版本会覆盖模型列表版本，且无法恢复，如需保留，请先备份模型列表中的版本", this.$t("prompt.title"), {
                    confirmButtonText: this.$t("confirm"),
                    cancelButtonText: this.$t("cancel"),
                    type: "warning",
                })
                .then(() => {
                    this.$http
                        .get(`/sys/vmc/recall/${id}`)
                        .then((res) => {
                            if (res.data.code !== 0) {
                                return this.$message.error(res.data.msg);
                            }
                            this.$message({
                                message: '回溯成功,如需查看请至模型列表',
                                type: "success",
                                duration: 2500,
                                onClose: () => {},
                            });
                        });
                })
                .catch(() => {});
        },
    }
};
</script>
